<?php

namespace App\Libs\Common;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SmsController extends Controller
{

    /**
     * 发送 验证码 短信
     * @param $phone   手机号
     * @param $content 内容
     * @param $type    短信类型 : 默认为 通知; 为 1 时 验证 ;为 2 时 营销
     * @return bool    返回类型
     */
    public function send($phone,$content,$type = 0)
    {

       $param = $this->validate($phone,$type);

       if($param === true){

           $target = "http://api.chanzor.com/send";

           if(empty($type)){

               // 账号 app ID 通知短信
               $account  = "98ad64";

               // md5加密字母大写  app key
               $password = strtoupper(md5("xb5g21ye0o"));

           }else if($type == 1){

               //账号 app ID  验证短信
               $account  = "98ad57";

               // md5加密字母大写  app key
               $password = strtoupper(md5("b5wdnryep1"));
           }else if($type == 2){

               //账号 app ID 营销短信
               $account  = "98ad65";

               // md5加密字母大写  app key
               $password = strtoupper(md5("c7q1zui2oh"));
           }

           $post_data = "account=$account&password=$password&mobile=$phone&sendTime=&content=".rawurlencode("【绿悦出品】$content");

           $gets = $this->request_Sms($target,$post_data);

           // 以 { 分割转成数组
           $res = explode('{',$gets);
           //  在最前面添加 {
           $data = substr_replace("{",$res[1],1);
           // 解析
           $data = json_decode($data,true);

           // 状态为0 返回 true
           if($data['status'] == 0){
               return true;
           }else{
               // 返回错误信息
               return ['status'=>$data['status'],'msg'=>$data['desc']];

           }

       }else{

           //失败 返回错误信息
           return ['status'=>1002,'msg'=>$param];
       }

    }


    /**
     * 验证
     * @param $phone 手机号
     * @param $type  短信类型
     * @return bool  返回布尔
     */
    function validate($phone,$type)
    {
        // 获取时间
        $nowToday = date('Y-m-d');
        $time     = time();

        if(empty($phone)) return "手机号不能为空";

        // 手机号是否在admin_user表中存在
        $admUser = M('admin_user')->where("phone = '$phone' and status = 1")->count();
		
        if(admUser){
            return true;
        }else{

            // 获取ip
            $ip   = get_client_ip();

            $ucode = M('user_code');
            // 同一ip 总数
            $ip_Count    = $ucode->where("ip = '$ip' and type = '$type' and inactive = 0 and FROM_UNIXTIME(time,'%Y-%m-%d')='$nowToday'")->count();
            // 同一手机 总数
            $phone_Count = $ucode->where("phone = '$phone' and type = '$type' and inactive = 0 and FROM_UNIXTIME(time,'%Y-%m-%d')='$nowToday'")->count();

            if($ip_Count > 5) return "ip超过今日上限";

            if($phone_Count > 5) return "短信类型超过今日上限";

            $phone_Last = $ucode->where("phone='$phone' and inactive = 0 and type = '$type'")->order('time desc')->getField('time');

            $time = $time - $phone_Last;

            if($time < 60) return "短信间隔小于一分钟";

            return true;
        }

    }


    /**
     * @param string $url  url
     * @param string $param 参数
     * @return bool|mixed|string 返回类型
     */
    function request_Sms($url = '', $param = '')
    {
        if (empty($url) || empty($param)) return false;

        if (!is_string($param) && is_array($param))
        {
            $str='';

            foreach ($param as $key => $value)
            {
                $str.="$key=".urlencode($value)."&";
            }

            $param=trim($str,'&');
        }

        $ch = curl_init();//初始化curl

        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);

        $data = curl_exec($ch);//运行curl

        if($data === false)
        {
            return  'Curl error: ' . curl_error($ch);
        }

        curl_close($ch);

        return $data;
    }


}